package helper

import (
	"encoding/json"
	"io"

	"github.com/imroc/req/v3"
)

type Mstat struct {
	Data []struct {
		Id   int    `json:"id"`
		Url  string `json:"url"`
		Br   int    `json:"br"`
		Size int    `json:"size"`
		Md5  string `json:"md5"`
	} `json:"data"`
	Code int `json:"code"`
}

func Music(id int) Mstat {
	e3xdata := App.Music(id)
	do, err := req.C().R().
		SetHeaderNonCanonical("Content-Type", "application/x-www-form-urlencoded"). // Set one header
		SetHeader("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36").
		SetBody(e3xdata).
		Post("https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token=")
	defer func(Body io.ReadCloser) {
		_ = Body.Close()
	}(do.Body)
	all, err := io.ReadAll(do.Body)
	if err != nil {
		return Mstat{}
	}
	var Mdata Mstat
	err = json.Unmarshal(all, &Mdata)
	if err != nil {
		return Mstat{}
	}
	return Mdata
}
